package com.yc.cloud.openai.portal.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.cloud.common.basic.dto.BaseQueryParam;
import com.yc.cloud.common.oss.service.OssFileService;
import com.yc.cloud.openai.entity.ModelRecord;
import com.yc.cloud.openai.mapper.ModelRecordMapper;
import com.yc.cloud.openai.portal.dto.request.ModelRecordListParam;
import com.yc.cloud.openai.portal.service.ModelRecordService;
import com.yc.cloud.openai.vo.ModelRecordVo;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import lombok.val;

/**
 * <p>
 * 3D模型记录 服务实现类
 * </p>
 *
 * @author Yancy
 * @since 2025-02-16
 */
@Service
public class ModelRecordServiceImpl extends ServiceImpl<ModelRecordMapper, ModelRecord> implements ModelRecordService {

    private final OssFileService ossFileService;

    public ModelRecordServiceImpl(OssFileService ossFileService) {
        this.ossFileService = ossFileService;
    }

    @Override
    public IPage<ModelRecordVo> list(ModelRecordListParam param, BaseQueryParam queryParam) {
        val query = new QueryWrapper<ModelRecord>();
        IPage<ModelRecord> page;
        val clientIds = StrUtil.split(param.getClientId(), ",");
        if (clientIds.size() == 1) {
            query.eq(ModelRecord.CLIENT_ID, clientIds.get(0));
        } else {
            query.in(ModelRecord.CLIENT_ID, clientIds);
        }
        if (CollUtil.isNotEmpty(param.getStatusList())) {
            query.in(ModelRecord.STATUS, param.getStatusList());
        }
        // 排序
        if (StrUtil.isNotEmpty(queryParam.getSortBy())) {
            if (BaseQueryParam.ORDER_DESC.equals(queryParam.getOrder())) {
                query.orderByDesc(queryParam.getSortBy());
            } else if (BaseQueryParam.ORDER_ASC.equals(queryParam.getOrder())) {
                query.orderByAsc(queryParam.getSortBy());
            }
        }
        // 模糊查询
        if (StrUtil.isNotEmpty(queryParam.getKeyword())) {
            query.and(queryWrapper -> {

            });
        }
        // 分页
        if (queryParam.getPage()) {
            page = new Page<>(queryParam.getPageNum(), queryParam.getPageSize(), true);
        } else {
            page = new Page<>(queryParam.getPageNum(), baseMapper.selectCount(query), true);
        }
        val modelRecordPage = baseMapper.selectPage(page, query);
        return modelRecordPage.convert(this::convertVo);
    }

    @Override
    public ModelRecord create(ModelRecord param) {
        val modelrecord = new ModelRecord();
        BeanUtils.copyProperties(param, modelrecord);
        val saveFlag = save(modelrecord);
        return saveFlag ? modelrecord : null;
    }

    @Override
    public ModelRecordVo get(Long id) {
        val modelRecord = getById(id);
        return convertVo(modelRecord);
    }

    @Override
    public boolean delete(Long id) {
        return removeById(id);
    }

    @Override
    public ModelRecord update(Long id, ModelRecord param) {
        val modelrecord = new ModelRecord();
        BeanUtils.copyProperties(param, modelrecord);
        modelrecord.setId(id);
        val updateFlag = updateById(modelrecord);
        return updateFlag ? modelrecord : null;
    }

    @Override
    public ModelRecordVo convertVo(ModelRecord modelrecord) {
        if (modelrecord == null) {
            return null;
        }
        val vo = new ModelRecordVo();
        BeanUtils.copyProperties(modelrecord, vo);
        // 获取图生模型的参考图
        if (StrUtil.isNotEmpty(modelrecord.getSampleFileId())) {
            val sampleFile = ossFileService.getByFileId(modelrecord.getSampleFileId());
            if (sampleFile != null) {
                vo.setSampleFileUrl(sampleFile.getUrl());
            }
        }
        // 获取最终模型文件
        if (StrUtil.isNotEmpty(modelrecord.getUuid())) {
            val modelFileList = ossFileService.listByParentFileId(modelrecord.getUuid());
            vo.setModels(ossFileService.convertVo(modelFileList));
        }
        return vo;
    }

    @Override
    public List<ModelRecordVo> convertVo(List<ModelRecord> ModelRecordList) {
        if (CollUtil.isNotEmpty(ModelRecordList)) {
            return ModelRecordList.stream().map(this::convertVo).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    @Override
    public List<ModelRecord> listByStatus(List<String> statusList) {
        val query = new QueryWrapper<ModelRecord>();
        query.in(ModelRecord.STATUS, statusList);
        return list(query);
    }
}
